package com.atguigu.controller;

import com.atguigu.cloud.entites.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author BLADE
 * @description:
 * @data 2024/4/1511:19
 */
@RestController
public class OrderController {
    //public static final String PaymentSrv_URL="http://localhost:8001";
    public static final String PaymentSrv_URL="http://cloud-payment-service";//consul服务注册中心上的微服务名称
    @Resource
    private RestTemplate restTemplate;

    @PostMapping("/consumer/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO){
        return restTemplate.postForObject(PaymentSrv_URL+"/pay/add",payDTO,ResultData.class);
    }
    @GetMapping("/consumer/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id){
        return restTemplate.getForObject(PaymentSrv_URL+"/pay/getById/"+id,ResultData.class,id);
    }
    @GetMapping("/consumer/pay/get/info")
    public String getPayByConsul(){
        return restTemplate.getForObject(PaymentSrv_URL+"/pay/get/info",String.class);
    }
    @Resource
    private DiscoveryClient discoveryClient;
    @GetMapping("/consumer/discovery")
    public String discovery(){
        List<String> services=discoveryClient.getServices();
        for(String element:services){
            System.out.println("*****element:"+element);
        }
        System.out.println("===========================");
        List<ServiceInstance> instances=discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance instance:instances){
            System.out.println(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return instances.get(0).getServiceId()+"\t"+instances.get(0).getHost()+"\t"+instances.get(0).getPort()+"\t"+instances.get(0).getUri();
    }
}
